use tauri::{AppHandle, SystemTrayEvent};

use crate::state;
use crate::utils::window;

pub(crate) fn tray_event_handler(_: &AppHandle, event: SystemTrayEvent) {
    match event {
        SystemTrayEvent::MenuItemClick { .. } => {}
        SystemTrayEvent::LeftClick { .. } => {
            let window = state::application::application().window();
            if window.is_some() {
                let _ = window.unwrap().set_focus();

                return;
            }

            // 保留全局主窗口
            let window = window::build_main_window();
            if window.is_err() {
                log::error!("构建主窗口失败：{}", window.err().unwrap());

                return;
            }

            state::application::application_mut().set_window(Some(window.unwrap()));
        }
        SystemTrayEvent::RightClick { .. } => {}
        SystemTrayEvent::DoubleClick { .. } => {}
        _ => {}
    }
}